package com.cyc.basic.view;

/**
 * @Description:
 * @version 1.0
 * @author cyc
 * @date 2021/10/8 11:09
 */
public class BossTest3 {
//    static Object o2 = new Object();
//    static Object o1 = new Object();

    public static void main(String[] args) {
        Object o2 = new Object();
        Object o1 = new Object();
        //线程1拿到o1锁后, 睡一秒去拿o2的锁, 此时o2的锁已经被线程2占用, 并且线程2也需要去拿o1的锁, 此时两两都在等待
        new Thread(()->{
                synchronized (o1) {
                    System.out.println("线程1锁o1");
                    try {
                        Thread.sleep(1000);
                        synchronized (o2) {
                            System.out.println("线程1锁o2");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        }).start();

        new Thread(() -> {
            synchronized (o2) {
                System.out.println("线程2锁o2");
                synchronized (o1) {
                    System.out.println("线程2锁o1");
                }
            }
        }).start();
    }
}
